package com.tbit.main.service.impl;

import com.tbit.main.constant.EventType;
import com.tbit.main.constant.MachineAbnormalConstant;
import com.tbit.main.dao.core.MannedEventDao;
import com.tbit.main.pojo.MachineAbnormal;
import com.tbit.main.pojo.MannedEvent;
import com.tbit.main.pojo.TerAlarm;
import com.tbit.main.service.DataService;
import com.tbit.main.service.MachineAbnormalService;
import com.tbit.main.service.MannedEventService;
import com.tbit.main.service.TerAlarmService;
import com.tbit.main.util.DateTimeUtil;
import com.tbit.main.util.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author LMY
 * @create 2023-06-06 19:38
 */
@Service
public class MannedEventServiceImpl implements MannedEventService {

    @Autowired
    private MannedEventDao mannedEventDao;
    @Autowired
    private TerAlarmService terAlarmService;
    @Autowired
    private DataService dataService;
    @Autowired
    private MachineAbnormalService machineAbnormalService;

    @Override
    public MannedEvent getByMachineId(Integer machineId, Integer eventType) {
        Map<String, Object> map = new HashMap<>();
        map.put("machineId", machineId);
        map.put("eventType", eventType);
        return mannedEventDao.getByMachineId(map);
    }

    @Override
    public void delByMachineIdType(Integer machineId, Integer eventType) {
        Map<String, Object> map = new HashMap<>();
        map.put("machineId", machineId);
        map.put("eventType", eventType);
        mannedEventDao.delByMachineIdType(map);
    }

    @Override
    public void addMannedEvent(Integer machineId, Integer accountId, Integer mannedId, Integer eventType) {
        MannedEvent mannedEvent = new MannedEvent(machineId, accountId, mannedId, eventType, DateTimeUtil.getNowTime());
        mannedEventDao.addMannedEvent(mannedEvent);
    }

    @Override
    public void mannedEvent(Integer machineId, Integer eventType) {
        MannedEvent mannedEvent = getByMachineId(machineId, eventType);

        MachineAbnormal machineAbnormal = machineAbnormalService.getByMachineIdAndType(machineId, MachineAbnormalConstant.MACHINE_ABNORMAL_MANNED);
        MachineAbnormal machineAbnormalLose = machineAbnormalService.getByMachineIdAndType(machineId, MachineAbnormalConstant.MACHINE_ABNORMAL_MANNED_LOSE);
        if (eventType.equals(EventType.MANNED_ZR) || eventType.equals(EventType.MANNED_MZ_LX) ||
                eventType.equals(EventType.MANNED_GZ) || eventType.equals(EventType.MANNED_DD)) {
            LogUtil.info("添加事件：" + eventType);
            if (mannedEvent == null) {
                addMannedEvent(machineId, dataService.getMachine(machineId).getAccountId(),
                        dataService.getMannedId(machineId), eventType);
            }
        } else {
            LogUtil.info("删除事件：" + eventType);
            if (eventType.equals(EventType.MANNED_ZR_HF)) {
                MannedEvent mannedEventDb = getByMachineId(machineId, EventType.MANNED_ZR);
                if (mannedEventDb != null) {
                    delByMachineIdType(machineId, EventType.MANNED_ZR);
                }
            } else if (eventType.equals(EventType.MANNED_MZ)) {
                MannedEvent mannedEventDb = getByMachineId(machineId, EventType.MANNED_MZ_LX);
                if (mannedEventDb != null) {
                    delByMachineIdType(machineId, EventType.MANNED_MZ_LX);
                }
                if (machineAbnormalLose != null) {
                    machineAbnormalService.del(machineId, MachineAbnormalConstant.MACHINE_ABNORMAL_MANNED_LOSE);
                }
            } else if (eventType.equals(EventType.MANNED_YX)) {
                MannedEvent mannedEventDb = getByMachineId(machineId, EventType.MANNED_GZ);
                if (mannedEventDb != null) {
                    delByMachineIdType(machineId, EventType.MANNED_GZ);
                }

                if (machineAbnormal != null) {
                    MannedEvent mannedEventDbDl = getByMachineId(machineId, EventType.MANNED_DD);
                    if (mannedEventDbDl == null) {
                        machineAbnormalService.del(machineId, MachineAbnormalConstant.MACHINE_ABNORMAL_MANNED);
                    }
                }
            } else if (eventType.equals(EventType.MANNED_DL_ZC)) {
                MannedEvent mannedEventDb = getByMachineId(machineId, EventType.MANNED_DD);
                if (mannedEventDb != null) {
                    delByMachineIdType(machineId, EventType.MANNED_DD);
                }

                if (machineAbnormal != null) {
                    MannedEvent mannedEventDbDl = getByMachineId(machineId, EventType.MANNED_GZ);
                    if (mannedEventDbDl == null) {
                        machineAbnormalService.del(machineId, MachineAbnormalConstant.MACHINE_ABNORMAL_MANNED);
                    }
                }
            }
        }

        // 载人事件告警
        if (eventType.equals(EventType.MANNED_ZR) || eventType.equals(EventType.MANNED_ZR_HF)) {
            terAlarmService.create(new TerAlarm(dataService.getMachine(machineId).getMachineNO(), new Date(), eventType, "无"));
        }


        /* 添加异常 */
        if (eventType.equals(EventType.MANNED_GZ) || eventType.equals(EventType.MANNED_DD) && machineAbnormal == null) {
            machineAbnormalService.insert(new MachineAbnormal(null, machineId, null, null, MachineAbnormalConstant.MACHINE_ABNORMAL_MANNED, null, null, null));
        }

        if (eventType.equals(EventType.MANNED_MZ_LX) && machineAbnormalLose == null) {
            machineAbnormalService.insert(new MachineAbnormal(null, machineId, null, null, MachineAbnormalConstant.MACHINE_ABNORMAL_MANNED_LOSE, null, null, null));
        }

    }
}
